$(".countbtn").click(function () {
    //获取数量
    var count = $(this).parents().siblings("#countdiv").children(".countinput").val();
    //获取哪个按钮点击了
    var btn = $(this).attr("name");
    if (btn == 'left'){
        count = Number(count) + 1;
    }else {
        if (count == 0){
            alert("不能再减啦。。。。。。。");
        } else{
            count = count - 1;
        }
    }
    //更新数量
    $(this).parents().siblings("#countdiv").children(".countinput").val(count);
    //更新总计
    var cprice = $(this).parents().parents().parents().siblings(".cpricetd").children("#cprice").html();
    var price = $(this).parents().parents().parents().siblings(".pricetd").children("#price");
    price.html(Number(cprice) * Number(count));
    textSum();

});

function textSum(){
    //更新总价
    var sumCustom = 0;
    $(".choosediv").each(function () {
        if (!($(this).children().is(":hidden"))){
            sumCustom += Number($(this).parent().nextAll(".pricetd").children().html());
        }
    });
    $("#sumCustom").val(Number(sumCustom));
}

$(".countinput").keyup(function () {

    var count = $(this).val();
    var cprice = $(this).parents().parents().parents().siblings(".cpricetd").children("#cprice").html();
    var price = $(this).parents().parents().parents().siblings(".pricetd").children("#price");
    if (Number(count) < 0){
        $(this).val("0");
        price.html("0");
    }else{
        price.html(Number(cprice) * Number(count));
    }
    textSum();
});

$(".tdchoosediv").click(function () {
    var sumCustom = 0;
    if ($(this).children().is(":hidden")){
        $(this).children().show();
        $(".chosetext").show();
        $(".pricetd").each(function () {
            sumCustom += Number($(this).children().html());
        });
        $("#sumCustom").val(sumCustom);
    }else{
        $(this).children().hide();
        $(".chosetext").hide();
        $("#sumCustom").val(sumCustom);
    }
})

$(".choosediv").click(function () {

    var sumCustom = $("#sumCustom").val();

    $(this).children().toggle();
    if (!($(".chosetext").is(":hidden") && $(".chosetextall").is(":hidden"))){
        $(".chosetextall").hide();
    }

    //隐藏了
    if ($(this).children().is(":hidden")){
        var thisprice = $(this).parent().nextAll(".pricetd").children().html();
        $("#sumCustom").val(Number(sumCustom) - thisprice);
    } else{
        var thisprice = $(this).parent().nextAll(".pricetd").children().html();
        $("#sumCustom").val(Number(sumCustom) + Number(thisprice));
    }

})

function jiesuan() {

    // $(".choosediv").each(function () {
    //
    //     if (!($(this).children().is(":hidden"))){
    //
    //         var pid = $(this).parent().nextAll(".comm").children().attr("id");
    //         var pvid = $(this).parent().nextAll(".cversion").children().attr("id");
    //         var pcid = $(this).parent().nextAll(".ccolor").children().attr("id");
    //         var count = $(this).parent().nextAll(".ccount").children().children("#countdiv").children().val();
    //
    //         $.post(
    //             "/ShoppingCartServlet",
    //             "method=saveShopCart&pid=" + pid + "&pcid=" + pcid + "&pvid=" + pvid + "&count=" + count,
    //             function (data) {
    //                 window.location.href = data;
    //             },
    //             "text"
    //         )
    //     }
    // });

    //[{"pid":1,"pcid":1,"pvid":1},{"pid":2,"pcid":2,"pvid":2}]
    var json = "[";
    $(".choosediv").each(function () {

        if (!($(this).children().is(":hidden"))){

            var pid = $(this).parent().nextAll(".comm").children().attr("id");
            var pvid = $(this).parent().nextAll(".cversion").children().attr("id");
            var pcid = $(this).parent().nextAll(".ccolor").children().attr("id");
            var count = $(this).parent().nextAll(".ccount").children().children("#countdiv").children().val();
            json = json + "{\"pid\":" + pid + ",\"pvid\":" + pvid + ",\"pcid\":" + pcid +  ",\"count\":" + count + "},";
        }

    });
    json = json + "{\"pid\":0,\"pcid\":0,\"pvid\":0,\"count\":0}]";
    // window.location.href = "/ShoppingCartServlet?method=shopCart&json="+ json;

    $.post(
        "/ShoppingCartServlet",
        "method=shopCart&json=" + json,
        function (data) {
            window.location.href = data;
        },
        "text"
    )
}